package com.common.util;

import java.text.*;
import java.util.Calendar;



public class GenerateSequenceUtil {
 
    /** The FieldPosition. */
    private static final FieldPosition HELPER_POSITION = new FieldPosition(0);
 
    /** This Format for format the number to special format. */
    private final static NumberFormat numberFormat = new DecimalFormat("0000");
 
    /** This int is the sequence number ,the default value is 0. */
    private static int seq = 0;
 
    private static final int MAX = 9999;
 
    /**
     * 时间格式生成序列
     * @return String
     */
    public static synchronized String generateSequenceNo() {
        Calendar rightNow = Calendar.getInstance();
        Format dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        StringBuffer sb = new StringBuffer();
        dateFormat.format(rightNow.getTime(), sb, HELPER_POSITION);
        numberFormat.format(seq, sb, HELPER_POSITION);
        if (seq == MAX) {
            seq = 0;
        } else {
            seq++;
        }
        return sb.toString();
    }
    
    /**
     * 根据roomId生成主键
     * @param appendKey
     * @return
     */
    public static synchronized String generateSequenceNo(String appendKey) {
        Calendar rightNow = Calendar.getInstance();
        StringBuffer sb = new StringBuffer();
        Format dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        dateFormat.format(rightNow.getTime(), sb, HELPER_POSITION);
        numberFormat.format(seq, sb, HELPER_POSITION);
        if (seq == MAX) {
            seq = 0;
        } else {
            seq++;
        }
        sb.append(appendKey);
        return sb.toString();
    }
    
}
